//
//  UIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/27.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

//给UIBarButtonItem扩展便利构造函数
//提示:指定构造不能定义在extension

extension UIBarButtonItem {
    
    //便利构造函数
    convenience init(title: String, imageName: String? = nil, target: Any?,action: Selector) {
        
        self.init()
        
        //自定义导航栏视图
        let button = UIButton()
        
        if let imgName = imageName {
            
            button.setImage(UIImage(named: imgName), for: .normal)
        }
        
        //添加点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        
        button.setTitle(title, for: .normal)
        
        //设置不同状态下的文字颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        //设置文字大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //设置和内容大小一样
        button.sizeToFit()
        
        //关联自定义button
        self.customView = button
    }
    
    
    
    
    
    
    
    
    
}
